package com.framework.modules.appdiscover;

import com.framework.entity.or.AppDiscoverArticle;
import com.framework.entity.or.AppDiscoverCover;
import com.framework.entity.sys.SysDeptJumpUrl;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.SysDeptJumpUrlService;
import com.framework.service.app.AppDiscoverArticleService;
import com.framework.service.app.AppDiscoverCoverService;
import com.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 文章控制器
 *
 * @author wq
 */
@RequestMapping("sys/discover")
@RestController
public class AppDiscoverController extends AbstractController {

    @Autowired
    private AppDiscoverArticleService appDiscoverArticleService;

    @Autowired
    private AppDiscoverCoverService appDiscoverCoverService;


    @Autowired
    private SysDeptJumpUrlService sysDeptJumpUrlService;


    /**
     * 文章列表
     *
     * @param params
     * @return
     */
    @RequestMapping("list")
    public RespResult list(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        PageUtils page = appDiscoverArticleService.queryArticlePage(params);
        return RespResult.ok().put("page", page);

    }

    /**
     * 查询封面列表
     *
     * @param params
     * @return
     */
    @RequestMapping("imgList")
    public RespResult imgList(@RequestParam Map<String, Object> params) {
        PageUtils page = appDiscoverArticleService.queryImgListByArticleId(params);
        return RespResult.ok().put("page", page);
    }

    /**
     * 根据id修改url
     *
     * @param url
     * @param id
     * @return
     */
    @RequestMapping("updateCover")
    public RespResult updateCoverById(@RequestParam String url, @RequestParam Integer id) {
        appDiscoverCoverService.updateCoverById(url, id);
        return RespResult.ok();
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadDiscoverCover")
    public RespResult uploadBanner(@RequestParam("file") MultipartFile file) throws IOException {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(), 3);
        String prefix = sysDeptJumpUrl.getUploadUrl();
        String bannerUrl = "";
        String suffix;
        if (file != null) {
            //上传轮播图图
            suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            bannerUrl = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);
        }
        return RespResult.ok().put("url", bannerUrl);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("getArticleById/{id}")
    public RespResult getArticleById(@PathVariable Integer id) {
        AppDiscoverArticle article = appDiscoverArticleService.selectById(id);
        return RespResult.ok().put("article", article);
    }

    /**
     * 根据id修改
     *
     * @param article
     * @return
     */
    @RequestMapping("updateById")
    public RespResult updateById(@RequestBody AppDiscoverArticle article) {
        appDiscoverArticleService.updateById(article);
        return RespResult.ok();
    }

    /**
     * 添加 文章
     *
     * @param article
     * @return
     */
    @RequestMapping("add")
    public RespResult add(@RequestBody AppDiscoverArticle article) {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(), 3);
        article.setJumpUrl(sysDeptJumpUrl.getBannerJump());
        article.setDeptId(getDeptId());
        article.setSort(1);
        article.setState(1);
        article.setCreateTime(new Date());
        Integer id = appDiscoverArticleService.addArticle(article);

        return RespResult.ok().put("discoverId", id);
    }


    /**
     * 添加文章的封面
     */

    @RequestMapping("addCover")
    public RespResult addCover(@RequestParam("files") MultipartFile[] file, @RequestParam("discoverId") Integer discoverId) throws IOException {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(), 3);
        String prefix = sysDeptJumpUrl.getUploadUrl();
        String suffix;
        String imgUrl;
        AppDiscoverCover cover;
        Integer count = 0;
        for (MultipartFile multipartFile : file) {

            cover = new AppDiscoverCover();
            suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            imgUrl = OSSFactory.build().uploadSuffix(multipartFile.getBytes(), suffix, prefix);
            cover.setImgUrl(imgUrl);
            count++;
            cover.setSort(count);
            cover.setCreateTime(new Date());
            cover.setDiscoverId(discoverId);
            appDiscoverCoverService.insert(cover);
        }

        return RespResult.ok();
    }

    @RequestMapping("uploadHead")
    public RespResult uploadHead(@RequestParam("file") MultipartFile file) throws IOException {
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(), 3);
        String prefix = sysDeptJumpUrl.getUploadUrl();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String imgUrl = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);
        return RespResult.ok().put("url", imgUrl);
    }

    @RequestMapping("deleteArticleById/{articleId}")
    public RespResult deleteArticleById(@PathVariable("articleId") Integer articleId){
        appDiscoverArticleService.deleteArticleById(articleId);

        return RespResult.ok();
    }

}
